﻿Imports System.Data.SqlClient
Imports DTO

Public Class PBanHangDAO

    Shared Function themPhieu(phieuBH As DTO.PBanHangDTO) As String
        Dim cn As SqlConnection = CKetNoi.Connect

        Dim cmd As New SqlCommand
        cmd.CommandText = "usp_ThemPhieuHang"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
        cn.Open()
        Dim para As SqlParameter
        para = New SqlParameter("@Ngay", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.Ngay
        cmd.Parameters.Add(para)

        para = New SqlParameter("@MaNhanVien", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.MaNhanVien
        cmd.Parameters.Add(para)

        para = New SqlParameter("@MaKhachHang", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.MaKhachHang
        cmd.Parameters.Add(para)

        para = New SqlParameter("@NgayGiao", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.NgayGiao
        cmd.Parameters.Add(para)

        para = New SqlParameter("@MaDieuKhoan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.MaDieuKhoan
        cmd.Parameters.Add(para)

        para = New SqlParameter("@MaHinhThuc", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.MaHinhThuc
        cmd.Parameters.Add(para)

        para = New SqlParameter("@HanThanhToan", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.HanThanhToan
        cmd.Parameters.Add(para)

        para = New SqlParameter("@ChietKhau", SqlDbType.Float)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.ChietKhau
        cmd.Parameters.Add(para)

        para = New SqlParameter("@VAT", SqlDbType.Float)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.VAT
        cmd.Parameters.Add(para)

        para = New SqlParameter("@TongDonHang", SqlDbType.Float)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.TongDonHang
        cmd.Parameters.Add(para)

        para = New SqlParameter("@GhiChu", SqlDbType.NVarChar, -1)
        para.Direction = ParameterDirection.Input
        para.Value = phieuBH.GhiChu
        cmd.Parameters.Add(para)

        para = New SqlParameter("@MaPhieu", SqlDbType.VarChar, 13)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        cmd.ExecuteNonQuery()
        cn.Close()
        Dim maPhieu As String = cmd.Parameters("@MaPhieu").Value.ToString
        Return maPhieu
    End Function

    Shared Function themChiTietPhieu(dsChiTiet As ComponentModel.BindingList(Of DTO.ChiTietBanHangDTO), maPhieu As String) As Integer
        Dim cn As SqlConnection = CKetNoi.Connect
        Dim kq As Integer = 0
        cn.Open()
        For Each ct As ChiTietBanHangDTO In dsChiTiet
            Dim cmd As New SqlCommand
            cmd.CommandText = "usp_ThemTCBanHang"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cn

            Dim para As SqlParameter
            para = New SqlParameter("@MaPhieu", SqlDbType.VarChar, 13)
            para.Direction = ParameterDirection.Input
            para.Value = maPhieu
            cmd.Parameters.Add(para)

            para = New SqlParameter("@MaHang", SqlDbType.VarChar, 7)
            para.Direction = ParameterDirection.Input
            para.Value = ct.MaHang
            cmd.Parameters.Add(para)

            para = New SqlParameter("@MaKho", SqlDbType.VarChar, 7)
            para.Direction = ParameterDirection.Input
            para.Value = ct.MaKho
            cmd.Parameters.Add(para)

            para = New SqlParameter("@SoLuong", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = ct.SoLuong
            cmd.Parameters.Add(para)

            para = New SqlParameter("@ThanhTien", SqlDbType.Float)
            para.Direction = ParameterDirection.Input
            para.Value = ct.ThanhTien
            cmd.Parameters.Add(para)

            para = New SqlParameter("@ChietKhau", SqlDbType.Float)
            para.Direction = ParameterDirection.Input
            para.Value = ct.ChietKhau
            cmd.Parameters.Add(para)

            para = New SqlParameter("@ThanhToan", SqlDbType.Float)
            para.Direction = ParameterDirection.Input
            para.Value = ct.ThanhToan
            cmd.Parameters.Add(para)

            para = New SqlParameter("@DonGia", SqlDbType.Float)
            para.Direction = ParameterDirection.Input
            para.Value = ct.DonGia
            cmd.Parameters.Add(para)

            kq = kq + (cmd.ExecuteNonQuery() / 2)
        Next
        cn.Close()
        Return kq
    End Function

End Class
